package com.redwood.report.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by daiye on 2016/12/29 0029.
 */

public abstract class BaseLazyFragment extends Fragment {
    View v;

    /** 第一次可见的标识 */
    private boolean isFirstVisible = true;
    /** 第一次不可见标识 */
    private boolean isFirstInvisible = true;
    /** 数据是否加载完成，由具体的子类来维护 */
    protected boolean isLoaded = false;
    /** 视图创建完成标识 */
    private boolean isPrepared;
    private Unbinder unbinder;
    /**
     * 如果Fragment创建需要数据，在这里接收传进来的数据。
     * 如果没有这个抽象方法就空实现。
     */
    protected abstract void managerArguments();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            managerArguments();
        }
        initData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        // 我们只在视图创建完成后，调用视图是否可见的方法
        if (isPrepared) {
            if (isVisibleToUser) {
                isFirstVisible();
            } else {
                isFirstInvisible();
            }
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (getLayoutId() != 0) {
            v = inflater.inflate(getLayoutId(), container, false);
        } else {
            v = super.onCreateView(inflater, container, savedInstanceState);
        }
        unbinder=ButterKnife.bind(this, v);
        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 视图准备完成
        isPrepared = true;
        // 视图是否可见
        //isVisibleToUser();
        // 初始化视图数据
        if (isLoaded()) {
            initViewData(v);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        //if (getUserVisibleHint()) {
            //onUserVisible();
        //}
        isVisibleToUser();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (getUserVisibleHint()) {
            onUserInvisible();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        // 视图销毁后将isPrepared重置
        isPrepared = false;
        //手动释放
        if(unbinder!=null) {
            unbinder.unbind();
        }

    }

    private void isVisibleToUser() {
        if (getUserVisibleHint()) {
            isFirstVisible();
        }
    }

    private void isFirstVisible() {
        if (isFirstVisible) {
            isFirstVisible = false;
            onFirstUserVisible();
        } else {
            onUserVisible();
        }
    }

    private void isFirstInvisible() {
        if (isFirstInvisible) {
            isFirstInvisible = false;
            onFirstUserInvisible();
        } else {
            onUserInvisible();
        }
    }

    private boolean isLoaded() {
        return isLoaded;
    }

    public void loaded() {
        isLoaded = true;
    }

    /**
     * initialized data but view is not initialized
     */
    protected abstract void initData();

    /**
     * when view created for the first time,we can set data on the View
     */
    protected abstract void initViewData(View v);

    /**
     * get LayoutId
     * @return
     */
    protected abstract int getLayoutId();

    /**
     * when fragment is visible for the first time, here we can do something Time-consuming operation like network access
     */
    protected abstract void onFirstUserVisible();

    /**
     * this method like the fragment's lifecycle method onResume()
     */
    protected abstract void onUserVisible();

    /**
     * when fragment is invisible for the first time
     */
    protected abstract void onFirstUserInvisible();
    /**
     * this method like the fragment's lifecycle method onPause()
     */
    protected abstract void onUserInvisible();

}